package com.walkline.screen;

import localization.RedFlowerResource;
import net.rim.blackberry.api.homescreen.HomeScreen;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;

import com.walkline.app.RedFlowerAppConfig;
import com.walkline.app.RedFlowerAppConfigException;
import com.walkline.ui.ForegroundManager;
import com.walkline.ui.RoundRectFieldManager;
import com.walkline.util.Enumerations.ShortcutKey;

public class SettingsScreen extends MainScreen implements RedFlowerResource
{
	private ForegroundManager _foreground;
	private CheckboxField checkboxUseEnglish;
	private ObjectChoiceField choiceShortcutKey;
	private EditField editNickName;

	private RedFlowerAppConfig _appConfig;
	private static ResourceBundle _resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

	public SettingsScreen(RedFlowerAppConfig appConfig)
	{
		super(USE_ALL_HEIGHT | NO_VERTICAL_SCROLL | NO_SYSTEM_MENU_ITEMS);
		setTitle(getResString(SETTINGS_TITLE));

		_appConfig = appConfig;

		RoundRectFieldManager managerLanguage = new RoundRectFieldManager();
		RoundRectFieldManager managerShortcutKey = new RoundRectFieldManager();
		RoundRectFieldManager managerNickName = new RoundRectFieldManager();

		_foreground=new ForegroundManager(0);
		
		//Language option start
		MyTitleLabelField labelLanguage = new MyTitleLabelField(getResString(SETTINGS_LANGUAGE));

		checkboxUseEnglish = new CheckboxField(getResString(SETTINGS_LANGUAGE_USEENGLISH), false, 134217728 | USE_ALL_WIDTH);
		
		MyDescriptionLabelField labelUseEnglishDescription = new MyDescriptionLabelField(getResString(SETTINGS_LANGUAGE_USEENGLISH_DESCRIPTION));

    	managerLanguage.add(labelLanguage);
    	managerLanguage.add(new SeparatorField());
    	managerLanguage.add(checkboxUseEnglish);
    	managerLanguage.add(labelUseEnglishDescription);
		//Lanaguage option end

    	//Shortcut key option start
    	MyTitleLabelField labelShortcutKey = new MyTitleLabelField(getResString(SETTINGS_SHORTCUT));

    	String[] choicesShortcutKeys = ShortcutKey.choicesShortcutKeys;
    	choicesShortcutKeys[0] = getResString(SETTINGS_SHORTCUT_NONE);
		choiceShortcutKey = new ObjectChoiceField(getResString(SETTINGS_SHORTCUT_KEY), choicesShortcutKeys, ShortcutKey.DEFAULT_KEY);
		
		MyDescriptionLabelField labelShortcutKeyDescription = new MyDescriptionLabelField(getResString(SETTINGS_SHORTCUT_KEY_DESCRIPTION));

    	managerShortcutKey.add(labelShortcutKey);
    	managerShortcutKey.add(new SeparatorField());
    	managerShortcutKey.add(choiceShortcutKey);
    	managerShortcutKey.add(labelShortcutKeyDescription);
    	//Shortcut key option end

    	//Nickname option start
    	MyTitleLabelField lblNickname = new MyTitleLabelField(getResString(SETTINGS_NICKNAME));

    	editNickName = new EditField("", "you", 12, EDITABLE | USE_ALL_WIDTH | NON_SPELLCHECKABLE);
    	editNickName.setPadding(6, 0, 0, 5);

    	managerNickName.add(lblNickname);
    	managerNickName.add(new SeparatorField());
    	managerNickName.add(editNickName);
    	//Nickname option end

		_foreground.add(managerLanguage);
		_foreground.add(managerShortcutKey);
		_foreground.add(managerNickName);
		add(_foreground);

		loadData();
	}
	
	private void loadData()
	{
		if (_appConfig != null)
		{
			checkboxUseEnglish.setChecked(_appConfig.isUseEnglish());
			choiceShortcutKey.setSelectedIndex(_appConfig.getShortcutKey());
			editNickName.setText(_appConfig.getNickname());
		} else{
			try {
				throw new RedFlowerAppConfigException(getResString(MESSAGE_ERROR_CANNOT_LOAD_SETTINGS));
			} catch (RedFlowerAppConfigException e) {}
		}	
	}

	private void saveData()
	{
		_appConfig.setUseEnglish(checkboxUseEnglish.getChecked());
		_appConfig.setShortcutKey(choiceShortcutKey.getSelectedIndex());
		_appConfig.setNickname(editNickName.getText());
		_appConfig.save();

		String nickName = _appConfig.getNickname();
		String newAppName = RedFlowerAppConfig.APP_NAME + nickName;

		if (_appConfig.getShortcutKey() > 0)
		{
			String shortcut_key = ShortcutKey.choicesShortcutKeys[_appConfig.getShortcutKey()];
			newAppName += "(" + shortcut_key + RedFlowerAppConfig.UNDERLINE + ")";
		}

		HomeScreen.setName(newAppName);
	}

	private String getResString(int key) {return _resources.getString(key);}

	public boolean onSave()
	{
		saveData();
    	return true;
	}
	
	class MyTitleLabelField extends LabelField
	{
		public MyTitleLabelField(String text)
		{
			super(text, USE_ALL_WIDTH | LabelField.ELLIPSIS);

			setFont(RedFlowerAppConfig.FONT_SETTINGS_TITLE);
		}

		protected void paint(Graphics g)
		{
			g.setColor(Color.DARKGRAY);
			super.paint(g);	
		}
	}

	class MyDescriptionLabelField extends LabelField
	{
		public MyDescriptionLabelField(String text)
		{
			super(text);

			setFont(RedFlowerAppConfig.FONT_SETTINGS_DESCRIPTION);
		}
	}
}